<template>
  <el-form ref="ruleForm" :model="form" label-width="100px" style="margin: 0 auto;width:100%;">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="目录">
          <el-select v-model="form.xftId" style="width:98%;" :disabled="true">
            <template>
              <el-option v-for="item in ggTypes" :key="item.id" :label="item.typeName" :value="item.id" />
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="文件" prop="gysId">
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-upload"
            @click="onButtonEdit2()"
          >查看文件</el-button>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="文件标签">
          <el-input v-model="form.fileLabel" :disabled="true" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="审批意见">
          <el-input v-model="comments" type="textarea" :autosize="{ minRows: 2, maxRows: 2}" />
        </el-form-item>
      </el-col>
    </el-row>
    <div style="text-align:center;">
      <el-button type="primary" @click="approval('ruleForm')">审批通过</el-button>
      <el-button type="danger" @click="back('ruleForm')">审批驳回</el-button>
    </div>

    <el-dialog :visible.sync="xmViewVisible" append-to-body width="640px" title="预览">
      <xm-view v-if="xmViewVisible" ref="xmView" :proid="code" :visible.sync="xmViewVisible" />
    </el-dialog>

    <el-dialog :visible.sync="viewPdfVisible" append-to-body width="1200px" title="预览">
      <view-pdf v-if="viewPdfVisible" ref="viewPdf" :proid="code" :visible.sync="viewPdfVisible" />
    </el-dialog>

  </el-form>
</template>

<script>

import { getAllLeafNode } from '@/api/projectCatalog'
import { getInfo } from '@/api/projectFile'
import { approval } from '@/api/approvalComments'
import { Message } from 'element-ui'

import XmView from '../zlgl/viewPhoto' // eslint-disable-line no-unused-vars
import ViewPdf from '../zlgl/viewPdf'

export default {
  components: { XmView, ViewPdf },
  inject: ['getList'],
  props: { // 第二种方式
    xiangmu: {
      type: String,
      required: true
    },
    proid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {},
      comments: '',
      name: '',
      oldId: '',
      suffix: '',
      fileLabel: '',
      xmViewVisible: false,
      viewPdfVisible: false,
      disabled: false,
      ggTypes: []
    }
  },
  mounted() {
    this.getAllLeafNode()
  },
  methods: {
    getAllLeafNode() {
      getAllLeafNode({ xiangmuId: this.xiangmu }).then(response => {
        this.ggTypes = response.data
      }).catch(function() {
      })
      this.getDetailed()
    },
    getDetailed() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      getInfo({ code: this.proid }).then(response => {
        this.form = response.data[0]
        this.oldId = response.data[0].id
        this.suffix = response.data[0].suffix

        loading.close()
      }).catch(function() {
        loading.close()
      })
    },
    // 查看图片的点击事件
    onButtonEdit2() {
      this.code = this.oldId
      if (this.suffix === 'png' || this.suffix === 'jpeg' || this.suffix === 'jpg') {
        this.xmViewVisible = true
        this.$nextTick(() => {
          this.$refs.xmView
        })
      } else if (this.suffix === 'pdf' || this.suffix === 'docx' || this.suffix === 'doc') {
        this.viewPdfVisible = true
        this.$nextTick(() => {
          this.$refs.viewPdf
        })
      }
    },
    // 确认添加问题
    async approval() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })

          approval({ otherId: this.oldId, comments: this.comments, approvalStatus: '2', flag: 'XF' }).then(response => {
            Message({
              message: '审批通过成功',
              type: 'success',
              duration: 5 * 1000
            })
            this.$emit('update:visible', false)
            this.getList()
            loading.close()
          }).catch(response => {
            this.getList()
            loading.close()
          })
        } else {
          return false
        }
      })
    },
    // 确认添加问题
    async back() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })

          var submitData = []
          submitData.otherId = this.oldId
          submitData.comments = this.comments
          submitData.approvalStatus = '3'
          approval(submitData).then(response => {
            Message({
              message: '审批驳回成功',
              type: 'success',
              duration: 5 * 1000
            })
            this.$emit('update:visible', false)
            this.getList()
            loading.close()
          }).catch(response => {
            this.getList()
            loading.close()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
